package com.ebt.m.customer.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.event.TapeViewEvent;
import com.ebt.m.customer.model.BeanCustomerTapeWrapper;
import com.ebt.m.customer.model.CustomerTapeJsonWrapper;
import com.ebt.m.customer.util.CustomerConvertUtil;
import com.ebt.m.data.bean.BeanCustomerTape;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * created by rick.wang
 */
public class TapeView extends LinearLayout implements View.OnClickListener, MediaPlayer.OnCompletionListener {

    enum State {
        Idle, Pause, Playing
    }

    private BeanCustomerTapeWrapper mTapeWrapperLocal;
    private State mState = State.Idle;
    private float mProgress = 0f;//[0,1]
    private TextView tape_message;
    private ImageView tape_delete;

    public TapeView(Context context) {
        super(context);
        init(context);
    }

    public TapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View contentView = inflate(context, R.layout.tape_view, this);
        tape_message = (TextView) contentView.findViewById(R.id.tape_message);
        tape_delete = (ImageView) contentView.findViewById(R.id.tape_delete);
        tape_message.setOnClickListener(this);
        tape_delete.setOnClickListener(this);
    }

    public void update(BeanCustomerTapeWrapper tapeWrapper, int maxWidthPx) {
        if (tapeWrapper == null) {
            Log.d("TapeView", "exception in tapeview, tapeWrapper is null!");
        }
        mTapeWrapperLocal = tapeWrapper;
        int width = getWidthByMaxWidthPx(mTapeWrapperLocal.getTape().getTapeDuration(), maxWidthPx);
        ViewGroup.LayoutParams params = tape_message.getLayoutParams();
        params.width = width;
        tape_message.setLayoutParams(params);
        tape_message.setText(String.valueOf(mTapeWrapperLocal.getTape().getTapeDuration()) + "\"");
    }

    public void update(CustomerTapeJsonWrapper tapeWrapper, int maxWidthPx) {

        BeanCustomerTape beanCustomerTape = CustomerConvertUtil.convertCustomerTapeJson2BeanCustomerTape(tapeWrapper.getTape());
        BeanCustomerTapeWrapper.State state = BeanCustomerTapeWrapper.State.ORIGINAL;
        switch (tapeWrapper.getState()){
            case ORIGINAL:
                state = BeanCustomerTapeWrapper.State.ORIGINAL;
                break;
            case DELETE:
                state = BeanCustomerTapeWrapper.State.DELETE;
                break;
            case ADD:
                state = BeanCustomerTapeWrapper.State.ADD;
                break;
        }
        mTapeWrapperLocal = new BeanCustomerTapeWrapper(beanCustomerTape, state);
        update(mTapeWrapperLocal, maxWidthPx);
    }


    private int getWidthByMaxWidthPx(int duration, int maxWidPx) {
        float ratio = ((float) duration) / 60;
        if (ratio > 1) {
            return maxWidPx;
        }
        return (int) (0.3 * maxWidPx + 0.7 * maxWidPx * ratio);
    }

    public void setProgress(float progress) {
        if (0 <= progress && progress <= 1) {
            mProgress = progress;
        }
    }

    public float getProgress() {
        return mProgress;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tape_message:
                clickMessage();
                break;
            case R.id.tape_delete:
                clickDelete();
                break;
        }
    }

    private void clickMessage() {
        switch (mState) {
            case Idle:
                EventBus.getDefault().post(new TapeViewEvent(mTapeWrapperLocal.getTape().getUuid()));
                if (mTapeOperate != null) {
                    mTapeOperate.onTapeToPlay(this, mTapeWrapperLocal, mProgress);
                }
                mState = State.Playing;
                break;
            case Pause:
                EventBus.getDefault().post(new TapeViewEvent(mTapeWrapperLocal.getTape().getUuid()));
                if (mTapeOperate != null) {
                    mTapeOperate.onTapeToPlay(this, mTapeWrapperLocal, mProgress);
                }
                mState = State.Playing;
                break;
            case Playing:
                if (mTapeOperate != null) {
                    mTapeOperate.onTapeToPause(this, mTapeWrapperLocal, mProgress);
                }
                mState = State.Pause;
                break;
        }
    }

    private void clickDelete() {
        if (mTapeOperate != null) {
            mTapeOperate.onTapeToDelete(this, mTapeWrapperLocal);
        }
    }

    public interface TapeOperate {
        void onTapeToDelete(TapeView view, BeanCustomerTapeWrapper tapeWrapper);

        void onTapeToPlay(TapeView view, BeanCustomerTapeWrapper tapeWrapper, float progress);

        void onTapeToPause(TapeView view, BeanCustomerTapeWrapper tapeWrapper, float progress);
    }

    private TapeOperate mTapeOperate;

    public void setTapeOperate(TapeOperate operate) {
        mTapeOperate = operate;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(TapeViewEvent event) {
        if (event != null && mTapeWrapperLocal != null && mTapeWrapperLocal.getTape() != null
                && !mTapeWrapperLocal.getTape().getUuid().equals(event.uuid)) {
            stopPlayingForUI();
            stopPlayingForPlayer();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (mState != State.Idle) {
            mState = State.Idle;
        }
    }

    private void stopPlayingForUI() {
        if (mState != State.Pause) {
            //TODO
//            tape_iv.setImageResource(R.drawable.tape_play_3);
            mState = State.Pause;
        }
    }

    private void stopPlayingForPlayer() {
        //TODO
    }

    /**
     * 隐藏删除按钮
     */
    public void hideDeleteBtn() {
        tape_delete.setVisibility(GONE);
    }

}
